package com.congee02.sw.var;

public class LongestSubarray {

    public int longestSubarray(int[] nums) {

        int n = nums.length;

        if (n == 1) {
            return 0;
        }

        // l = r = 0
        boolean f = nums[0] == 0;
        int zeroCnt = f ? 1 : 0;

        int ans = f ? 0 : 1;

        int l = 0;
        for (int r = 1 ; r < n ; r ++ ) {
            if (nums[r] == 0) {
                zeroCnt ++;
            }
            while (zeroCnt > 1) {
                if (nums[l ++] == 0) {
                    zeroCnt --;
                }
            }
            ans = Math.max(ans, r - l);
        }

        return ans;
    }

}
